import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 是否有角色
 * @param {*} key
 */
export function hasRole (key) {
  return JSON.parse(sessionStorage.getItem('roles') || '[]').indexOf(key) !== -1 || false
}


/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

export function formatDateTime(cellValue) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}

export function formatDate(cellValue) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '-' + month + '-' + day
}

//处理默认选中当前日期
export function getNowDate() {
   var now = new Date();
   var year = now.getFullYear(); //得到年份
   var month = now.getMonth(); //得到月份
   var date = now.getDate(); //得到日期
   month = month + 1;
   month = month.toString().padStart(2, "0");
   date = date.toString().padStart(2, "0");
   var defaultDate = `${year}-${month}-${date}`;
   console.log(defaultDate)
   return defaultDate;
   // this.$set(this.info, "stockDate", defaultDate);
}

export function copyText(copytext) {
    const text = document.createElement('input'); // 创建节点
    text.setAttribute('readonly', 'readonly');
    text.value = copytext; // 赋值
    document.body.appendChild(text); // 插入节点
    text.setSelectionRange(0, text.value.length);
    text.select(); // 选中节点
    document.execCommand('copy'); // 执行浏览器复制方法
    if (document.body.removeChild(text)) {
        this.$message({ type: 'success', message: '复制成功' })
    } else {
        this.$message({ type: 'error', message: '复制失败' })
    }
}
